<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成绩列表</title>
<!-- 下载并引入jquery easyUI -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>


</head>
<body>
	<!-- 数据列表  start -->
	<!-- 在页面中使用easy ui组件进行界面编程 ,数据表格组件 -->
	<table id="score_dg" title="成绩列表" class="easyui-datagrid"
		style="width: 1150px; height: 480px"
		url="${param.request.contextPath}/admin/scorelist" toolbar="#score_toolbar"
		pagination="true" rownumbers="true" fitColumns="true"
		singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<!--  field字段需要与返回数据的属性名一致 -->
				<th field="scoId" align="center">编号</th>
				<th field="times" width="50">考试时间</th>
				<th field="subScore" width="50">考试成绩</th>
				<!-- <th field="category.categoryName" width="50">商品分类</th> -->
				<!-- 在列表中显示多表关联数据，需要使用格式化器进行解析数据 -->
				<!-- 如果一个对象的值需要显示多列，field属性的名称不能相同，formatter的方法名字也不能相同 -->
				<th field="loc" formatter="classroomFormatter" width="50">考试地址</th>
				<th field="name" formatter="teacherFormatter" width="50">任课老师</th>
				<th field="stuName" formatter="studentFormatter" width="50">学生姓名</th>
				<th field="subName" formatter="subjectFormatter" width="50">课程名称</th>
			
			</tr>
		</thead>
	</table>
	<!-- 工具条组件 -->
	<div id="score_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newScore()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editScore()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyScore()">删除</a>
	</div>
	<!-- 数据列表 end  -->
	<!-- 新增和修改表单  start -->
	<div id="score_dlg" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#score_dlg-buttons">
		<div class="ftitle">成绩信息管理</div>
		<form id="score_fm" method="post">
		<div class="fitem">
				<label>考试科目:</label> <input id="socreSubject"
					name="subject.subId" class="easyui-combobox"
					data-options="valueField:'subId',textField:'subName',
					url:'${param.request.contextPath}/subject/list'">
			</div>
			<div class="fitem">
				<label>考试成绩:</label> <input name="subScore"
					class="easyui-validatebox" data-options="required:true, missingMessage:'请输入成绩',validType:['integer','maxLength[6]']">
			</div>			
			<div class="fitem">
				<label>授课老师:</label> <input id="socreTeahcer"
					name="teacher.tehId" class="easyui-combobox"
					data-options="valueField:'tehId',textField:'name',
					url:'${param.request.contextPath}/teacher/listAll'">
			</div>
			<div class="fitem">
				<label>考试地点:</label> <input id="socreClassroom"
					name="classroom.claroomId" class="easyui-combobox"
					data-options="valueField:'claroomId',textField:'loc',
					url:'${param.request.contextPath}/admin/classroomlistall'">
			</div>
			<div class="fitem">
				<label>学生姓名:</label> <input id="socreStudent"
					name="student.stuId" class="easyui-combobox"
					data-options="valueField:'stuId',textField:'stuName',
					url:'${param.request.contextPath}/student/all'">
			</div>
			<div class="fitem">
				<label>考试时间:</label> <input name="times" class="easyui-datebox" lass="easyui-validatebox"
					 required data-options="validType:'md[\'10/11/2012\']'">
			</div>
		</form>
	</div>
	<div id="score_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="saveScore()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#score_dlg').dialog('close')">取消</a>
	</div>
	<!-- 新增和修改表单 end -->
	<script type="text/javascript">
		//easy ui在处理多列数据时，需要使用格式化器解析数据
		function classroomFormatter(value, row, index) {
			//value是传递进来的category对象；row是当前行数据，index是第几行数据
			//return value.categoryName;
			if(row.classroom == null){
				return null;
				}
			return row.classroom.loc;
		}
	
	
		
		function teacherFormatter(value, row, index) {
			//value是传递进来的category对象；row是当前行数据，index是第几行数据
			//return value.categoryName;
			if(row.teacher == null){
				return null;
				}
			return row.teacher.name;
		}
		function studentFormatter(value, row, index) {
			//value是传递进来的category对象；row是当前行数据，index是第几行数据
			//return value.categoryName;
			if(row.student == null){
				return null;
				}
			return row.student.stuName;
		}
		function subjectFormatter(value, row, index) {
			//value是传递进来的category对象；row是当前行数据，index是第几行数据
			//return value.categoryName;
			if(row.subject == null){
				return null;
				}
			return row.subject.subName;
		}
		
		function newScore() {
			$('#score_dlg').dialog('open').dialog('setTitle', '新增成绩');
			$('#score_fm').form('clear');
			url = '${pageContext.request.contextPath}/admin/scoreadd';
			loadTeacherSuccess(0); 
			loadStudentSuccess(0);
			loadClassroomSuccess(0);
			loadSubjectSuccess(0);
		}
		function editScore() {
			$('#score_fm').form('clear');
			var row = $('#score_dg').datagrid('getSelected');
			if (row) {
				$('#score_dlg').dialog('open').dialog('setTitle', '修改成绩');
				$('#score_fm').form('load', row);
				url = '${pageContext.request.contextPath}/admin/scoreedit?scoId='
						+ row.scoId;
				loadTeacherSuccess(row.teacher.tehId);
				loadStudentSuccess(row.student.stuId);
				loadClassroomSuccess(row.classroom.claroomId);
				loadSubjectSuccess(row.subject.subId);
			}
		}
		//下拉列表反选的方法
		 function loadTeacherSuccess(tehId) {
			var data = $("#socreTeahcer").combobox("getData");
			
			if (data && data.length > 0) {
				if (tehId == 0) {
					$("#socreTeahcer")
							.combobox("setValue", data[0]['tehId']);
				} else {
					$("#socreTeahcer").combobox("setValue",tehId);
				}
			}
		}
		 function loadStudentSuccess(stuId) {
				var data = $("#socreStudent").combobox("getData");
				console.log(stuId);
				if (data && data.length > 0) {
					if (stuId == 0) {
						$("#socreStudent")
								.combobox("setValue", data[0]['stuId']);
					} else {
						$("#socreStudent").combobox("setValue", stuId);
					}
				}
			}
		 function loadClassroomSuccess(claroomId) {
				var data = $("#socreClassroom").combobox("getData");
				
				if (data && data.length > 0) {
					if (claroomId == 0) {
						$("#socreClassroom")
								.combobox("setValue", data[0]['claroomId']);
					} else {
						$("#socreClassroom").combobox("setValue", claroomId);
					}
				}
			}
		 function loadSubjectSuccess(subId) {
				var data = $("#socreSubject").combobox("getData");
			
				if (data && data.length > 0) {
					if (subId == 0) {
						$("#socreSubject")
								.combobox("setValue", data[0]['subId']);
					} else {
						$("#socreSubject").combobox("setValue", subId);
					}
				}
			}
		function saveScore() {
			$('#score_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}
					$('#score_dlg').dialog('close'); // close the dialog
					$('#score_dg').datagrid('reload'); // reload the user data
				}
			});
		}
		function destroyScore() {
			var row = $('#score_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除成绩?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/admin/scoredelete',
														{
															scoId : row.scoId
														},
														function(result) {
															if (result.success) {
																$('#score_dg')
																		.datagrid(
																				'reload'); // reload the user data
															} else {
																$.messager
																		.show({ // show error message
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}
	
	</script>
</body>
</html>